//------------------------------------------------------------------------------------------------
// The MIT License (MIT)
// 
// Copyright (c) 2019 Crsky
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//------------------------------------------------------------------------------------------------

#include "engine/base_types.h"
#include "engine/listen_server.h"
#include "engine/sys.h"

#include <windows.h>
#include <shlwapi.h>
#pragma comment( lib, "shlwapi.lib" )
#undef GetCommandLine

namespace launcher
{

bool GetRootDirectory( wstring& directory )
{
	HMODULE		module_handle;
	WCHAR		file_name[MAX_PATH];
	WCHAR		full_path[MAX_PATH];
	DWORD		attributes;

	// Get the handle of the *.exe module
	module_handle = GetModuleHandleW( nullptr );

	if ( module_handle == nullptr )
		return false;

	// Get the path of the *.exe file
	if ( GetModuleFileNameW( module_handle, file_name, _countof( file_name ) ) == 0 )
		return false;

	// Remove the *.exe file name in the path,
	// will got "c:\\game\\bin"
	PathRemoveFileSpecW( file_name );

	// The path may be a relative path, try convert to full absolute path
	if ( GetFullPathNameW( file_name, _countof( full_path ), full_path, nullptr ) == 0 )
		return false;

	// Verify the path
	attributes = GetFileAttributesW( full_path );

	if ( attributes == INVALID_FILE_ATTRIBUTES )
		return false;

	if ( !(attributes & FILE_ATTRIBUTE_DIRECTORY) )
		return false;

	// The direcotry is valid
	directory.assign( full_path );

	return true;
}

bool GetCommandLine( wstring& command_line )
{
	command_line.assign( GetCommandLineW() );
	return true;
}

int RunMain( HINSTANCE hInstance )
{
	wstring root_dir;
	wstring command_line;

	if ( !GetRootDirectory( root_dir ) )
	{
		ErrorExit( L"Unable to get the game directory" );
		return 1;
	}

	if ( !GetCommandLine( command_line ) )
	{
		ErrorExit( L"Unable to get the command line" );
		return 1;
	}

	return ListenServer()->Run( reinterpret_cast<intptr_t>(hInstance), root_dir, command_line );
}

}

int WINAPI wWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nShowCmd )
{
	UNREFERENCED_PARAMETER( hPrevInstance );
	UNREFERENCED_PARAMETER( lpCmdLine );
	UNREFERENCED_PARAMETER( nShowCmd );

	return launcher::RunMain( hInstance );
}
